package main

import (
	"fmt"
	"freedom/finterface"
	"freedom/flog"
	"freedom/fnet"
	"freedom/futils"
)

// 服务器链接
type ServerConn struct {
	ServerType int32
}

//调用OnConnect
func (s *ServerConn) OnConnect(conn finterface.IConnection) {
	str := fmt.Sprintf("serverType=%d", s.ServerType)
	conn.SendMsgData(1, 0, []byte(str))
	flog.Debug("Connect Send ", str)
	//flog.Debug("Connect")
}

func (s *ServerConn) OnDisconnect(conn finterface.IConnection) {
	flog.Debug("Disconnect ")
}

func main() {
	flog.InitLogger("client", 2, "./log", flog.DebugLevel)

	defer flog.SyncLogger()
	wsServer := fnet.NewWsServer("192.168.1.107", 7777, "/msg", nil, nil)

	svrConn := ServerConn{ServerType: 1}
	wsClient := fnet.NewWsClient("192.168.1.107", 9999, "/msg", true, &svrConn, wsServer)
	wsClient.ConnectWebSocket()

	futils.Graceful()

	//addr := fmt.Sprintf("%s:%d", "localhost", 9999)
	//u := url.URL{Scheme: "ws", Host: addr, Path: "/msg"}
	//flog.Debugf("connecting to %s", u.String())
	//c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
	//if err != nil {
	//	log.Fatal("dial:", err)
	//}
	//defer c.Close()
	//conn, err := websocket.Dial("ws://192.168.1.108:9999/msg", "msg", "http://freedom")
	//if err != nil {
	//	fmt.Println("dialing ", err.Error())
	//	return
	//}
	//fmt.Println("success")
	//if n, err := conn.Write([]byte("hello")); err != nil {
	//	fmt.Println("WebSocket handshake error: %v", err)
	//	return
	//} else {
	//	fmt.Println("send success n ", n)
	//}
	//var actual_msg = make([]byte, 512)
	//n, err := conn.Read(actual_msg)
	//if err != nil {
	//	fmt.Println("Read: %v", err)
	//	return
	//}
	//actual_msg = actual_msg[0:n]
	//fmt.Println(string(actual_msg))
	//conn.Close()
}
